/*
 *  CS202 - Spring 2009">
 *  Professor: Dr. Michael Peterson">
 *  Student: Deylo Woo (dwoo711@gmail.com) ">
 */


package project_3_v_2;

import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
/**
 * Game Main class inherited from JFrame
 * @author deylo
 */
public class GameMain extends JFrame {

    private GamePanel _gamePnl;
    private MenuPanel _menuPnl;
    private KeyPressHandler _keyHandler;
    /**
     * Contructor
     */
    public GameMain() {
        super("Game v2.0");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 800);

        _gamePnl = new GamePanel();
        this.add(_gamePnl, BorderLayout.CENTER);

        _menuPnl = new MenuPanel();
        this.add(_menuPnl, BorderLayout.SOUTH);

        _keyHandler = new KeyPressHandler();
        this.addKeyListener(_keyHandler);

        setVisible(true);
        center(this);


        while (true) {
            pauseGame();
            _gamePnl.move();
            _gamePnl.repaint();

            _menuPnl.repaint();
        _menuPnl.setScore("Score: " + _gamePnl.getScore());
        }
    }

    /**
     * will pause the iteration of the game for 20 milesecond
     */
    private static void pauseGame() {
        try {
            Thread.sleep(20); // pause for 20 ms
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }

    /**
     * Center the frame on screen
     * @param frame
     */
    public static void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();

        int w = frame.getWidth();
        int h = frame.getHeight();

        int x = center.x - w / 2, y = center.y - h / 2;
        frame.setBounds(x, y, w, h);
        frame.validate();
    }
    /**
     * Main method, entry point
     * @param args
     */
    public static void main(String[] args) {
        GameMain _game = new GameMain();
    }

    /**
     * Key listener class to implement Keys event handlers
     */
    private class KeyPressHandler implements KeyListener {

        /**
         * KeyTyped Method, not used
         * @param e
         */
        public void keyTyped(KeyEvent e) {
            // throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         * Key pressed event handler method
         * @param e: the key pressed
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == e.VK_SPACE) {
                _gamePnl.FireMissile();
            }
            if (e.getKeyCode() == e.VK_LEFT) {
                _gamePnl.MoveLeft();
            } else if (e.getKeyCode() == e.VK_RIGHT) {
                _gamePnl.MoveRight();
            }
        }

        /**
         * Key released this method is not used.
         * @param e
         */
        public void keyReleased(KeyEvent e) {
            // throw new UnsupportedOperationException("Not supported yet.");
        }
    }
}
